package cn.wolfcode.ws.mq.websocket;

import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@Setter
@Getter
//相当于我们之前@RequestMappering注解的作用,用来接ws://请求
@ServerEndpoint("/{uuid}")
@Component
public class WebSocketServer {
    //保存会话消息
    private Session session;
    public static ConcurrentHashMap<String,WebSocketServer> clients = new ConcurrentHashMap<>();
    /**
     * onOpen:当客户端和服务端建立连接了  执行onOpen方法
     * onClose:当客户端和服务端断开了连接 会执行onClose方法
     * onMessag:当客户端往服务端发送数据的时候会触发onMessage方法
     * onError:出错了  会进入onError;
     * @param session
     * @param uuid
     */
    @OnOpen
    public void onOpen(Session session, @PathParam( "uuid") String uuid){

        System.out.println("66666666666===>"+uuid);

        this.session = session;

        clients.put(uuid,this);

    }

    @OnClose

    public void onClose(@PathParam( "uuid") String uuid){

        clients.remove(uuid);

    }

    @OnError

    public void onError(Throwable error) {

        error.printStackTrace();

    }

}